import { ref, reactive } from 'vue'
import { goDialog, httpErrorHandle } from '@/utils'
import { DialogEnum } from '@/enums/pluginEnum'
import { Chartype, ChartList } from '../../../index.d'
// import { projectListApi, deleteProjectApi, changeProjectReleaseApi } from '@/api/path'
import { ResultEnum } from '@/enums/httpEnum'

// 数据初始化
export const useDataListInit = () => {
  const loading = ref(true)

  const paginat = reactive({
    // 当前页数
    page: 1,
    // 每页值
    limit: 12,
    // 总数
    count: 10
  })

   const list = ref<ChartList>([
    {
      id: 1,
      title: '测试数据不可用',
      release: true,
      label: '官方案例'
    } as Chartype,
  ])

  // 删除处理
  const deleteHandle = (cardData: object, index: number) => {
    goDialog({
      type: DialogEnum.DELETE,
      promise: true,
      onPositiveCallback: () =>
        new Promise(res => setTimeout(() => res(1), 1000)),
        // new Promise(res => {
        //  // 这里可以调用删除项目的接口
        //   res(
        //     deleteProjectApi({
        //       ids: cardData.id
        //     })
        //   )
        // }),
      promiseResCallback: (res: any) => {
        if (res.code === ResultEnum.SUCCESS) {
          // window['$message'].success(window['$t']('global.r_delete_success'))
          window.$message.success('删除成功')
          list.value.splice(index, 1)
          // fetchList() // 重新请求数据
          return
        }
        httpErrorHandle()
      }
    })
  }
   // 数据请求
  const fetchList = async () => {
    loading.value = true
    // const res = await projectListApi({
    //   page: paginat.page,
    //   limit: paginat.limit
    // })
    // 分页查询 
    const res = {
      code: 200,
      data: [
        {
          id: 1,
          projectName: '测试项目1',
          state: 1,
          createTime: '2023-10-01',
          indexImage: 'project/moke-20211219181327.png',
          createUserId: 'user1',
          title: '测试数据不可用',
          release: true,
          label: '官方案例'
        },
        {
          id: 2,
          projectName: '测试项目2',
          state: -1,
          createTime: '2023-10-02',
          createUserId: 'user2',
          label: '官方案例'
        }
      ],
      count: 2
    }
    console.log('模拟请求数据进行分页', res);
    
    if (res && res.data) {
      const { count } = res as any // 这里的count与data平级，不在Response结构中
      paginat.count = count
      list.value = res.data.map(e => {
        const { id, projectName, state, createTime, indexImage, createUserId } = e
        return {
          id: id,
          title: projectName,
          createId: createUserId,
          time: createTime,
          image: indexImage || '',
          release: state !== -1
        }
      })
      setTimeout(() => {
        loading.value = false
      }, 500)
      return
    }
    httpErrorHandle()
  }

  // 修改页数
  const changePage = (_page: number) => {
    paginat.page = _page
    fetchList()
  }

  // 修改大小
  const changeSize = (_size: number) => {
    paginat.limit = _size
    fetchList()
  }

  // 发布处理
  const releaseHandle = async (cardData: Chartype, index: number) => {
    const { id, release } = cardData
    // const res = await changeProjectReleaseApi({
    //   id: id,
    //   // [-1未发布, 1发布]
    //   state: !release ? 1 : -1
    // })
    // 模拟发布接口
    console.log("模拟发布接口", cardData);    
    const res = {
      code: 200
    }
    if (res && res.code === ResultEnum.SUCCESS) {
      list.value = []
      fetchList()
      // 发布 -> 未发布
      if (release) {
        window['$message'].success(window['$t']('global.r_unpublish_success'))
        return
      }
      // 未发布 -> 发布
      window['$message'].success(window['$t']('global.r_publish_success'))
      return
    }
    httpErrorHandle()
  }

  // 立即请求
  fetchList()

  return {
    loading,
    paginat,
    list,
    fetchList,
    releaseHandle,
    changeSize,
    changePage,
    deleteHandle
  }
}
